/*
 *
 *  *  Copyright (c), utrix (idea110@163.com).
 *  *  <p>
 *  *  Licensed under the GNU Lesser General Public License 3.0 (the "License");
 *  *  you may not use this file except in compliance with the License.
 *  *  You may obtain a copy of the License at
 *  *  <p>
 *  * https://www.gnu.org/licenses/lgpl.html
 *  *  <p>
 *  * Unless required by applicable law or agreed to in writing, software
 *  * distributed under the License is distributed on an "AS IS" BASIS,
 *  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  * See the License for the specific language governing permissions and
 *  * limitations under the License.
 *
 */

package priv.utrix.micro.utils;

import cn.hutool.core.lang.Assert;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.digest.HMac;
import cn.hutool.crypto.symmetric.DES;

/**
 * 加解密全局通用工具类
 *
 * @author utrix
 * @since 2020/10/26 21:50
 */
public final class CryptoUtil {
    private static final String ERROR_MESSAGE = "The content to be encrypted is null";
    /**
     * 对称加密方法des
     */
    private static final DES DES_CRYPTO = SecureUtil.des();
    /**
     * 摘要加密之HMAC-MD5
     */
    private static final HMac MD5_CRYPTO = SecureUtil.hmacMd5("Hello.O12XH");

    /**
     * 对数据进行HmacMd5算法不可逆加密，固定长度32
     *
     * @param str 需要加密的数据
     * @return java.lang.String
     * @since 2020/10/26 22:18
     */
    public static String encryptHmacMd5(String str) {
        Assert.notNull(str, ERROR_MESSAGE);
        return MD5_CRYPTO.digestHex(str);
    }

    /**
     * 对数据进行Md5算法不可逆加密，固定长度32
     *
     * @param str 需要加密的数据
     * @return java.lang.String
     * @since 2020/10/26 22:18
     */
    public static String encryptMd5(String str) {
        Assert.notNull(str, ERROR_MESSAGE);
        return SecureUtil.md5(str);
    }


    /**
     * 对数据进行加密
     *
     * @param str 需要加密的数据
     * @return java.lang.String
     * @since 2020/10/26 22:18
     */
    public static String encryptDES(String str) {
        Assert.notNull(str, ERROR_MESSAGE);
        return DES_CRYPTO.encryptHex(str);
    }

    /**
     * 对数据进行解密
     *
     * @param str 待解密数据
     * @return java.lang.String
     * @since 2020/10/26 22:18
     */
    public static String decryptDES(String str) {
        Assert.notNull(str, ERROR_MESSAGE);
        return DES_CRYPTO.decryptStr(str);
    }


    private CryptoUtil() {}
}
